<?php
class Claycode_configuration
{
	private static $o_instance;
	private $arr_applications;
	private $arr_forms;
	private $s_appRequestVarName;
	private $s_subAppRequestVarName;
	private $s_defaultController;
	private $s_formIdentField;
	protected $s_encryptionKey;
	private $arr_currAppConf;
	private $s_formSubmitHandlerField;
	private $s_formSessionArraySubmitHandler;
	private $s_serviceRequestParam;
	private $arr_services;
	
	public function __construct()
	{
		include C_FW_customPackage . 'configs/applications.php';
		include C_FW_customPackage . 'configs/forms_config.php';
		include C_FW_customPackage.'configs/defaults.php';
		include C_FW_customPackage . 'configs/db_config.php';
		include C_FW_customPackage . 'configs/services.php';
		$this->s_formSessionArraySubmitHandler = $s_formSessionArraySubmitHandler;
		$this->s_formSubmitHandlerField = $s_formPostSubmitHandlerField;
		$this->s_defaultController = $s_defaultController;
		$this->arr_forms = $arr_forms;
		$this->s_formIdentField = $s_formIdentField;
		$this->arr_applications = $arr_applications;
		$this->s_appRequestVarName = $s_appRequestVarName;
		$this->s_subAppRequestVarName = $s_subAppRequestVarName;
		$this->s_encryptionKey = $s_encryptionKey;
		
		//services config
		$this->s_serviceRequestParam = $s_serviceRequestParam;
		$this->arr_services = $arr_services;
	}
	
	public function getServiceRequestParam()
	{
		return $this->s_serviceRequestParam;
	}
	
	public function getServiceConfigs()
	{
		return $this->arr_services;
	}
	
	public function getServiceConfig($s_serviceAlias)
	{
		if(isset($this->arr_services[$s_serviceAlias])){
			//existing service
			return $this->arr_services[$s_serviceAlias];
		}else{
			return false;
		}
	}
	
	public function setCurrentAppConf($arr_currAppConf)
	{
		$this->arr_currAppConf = $arr_currAppConf;
	}
	
	public function getCurrentAppIdent()
	{
		$arr_appConf = $this->getCurrentAppConf();
		return $arr_appConf['app_ident'];
	}
	
	public function getCurrentAppConf()
	{
		$s_currAppIdent = Claycode_Router::getInstance()->arr_currAppConf['app_ident'];
		$arr_currAppConf = array();
		$arr_currAppConf = $this->getAppConf($s_currAppIdent, 'app_ident');
		return $arr_currAppConf;
	}
	
	public function getAppConfByIdent($s_currAppIdent)
	{
		return $this->getAppConf($s_currAppIdent, 'app_ident');
	}
	
	private function getAppConf($s_searchString, $s_searchKey)
	{
		$arr_currAppConf = array();
		foreach($this->arr_applications as $arr_appConfig){
			if($arr_appConfig[$s_searchKey] == $s_searchString){
				$arr_currAppConf = $arr_appConfig;
				break;
			}
		}
		
		return $arr_currAppConf;
	}
	
	public function getEncryptionKey()
	{
		return $this->s_encryptionKey;
	}
	
	public function getDefaultController()
	{
		return $this->s_defaultController;
	}
	
	public function getApplicationsArray()
	{
		return $this->arr_applications;
	}
	
	public function getAppConfArray($s_controllerClassAlias)
	{
		$arr_appConfArray = array();
		foreach($this->arr_applications as $arr_appConfig){
			if($arr_appConfig['controller_class_alias'] == $s_controllerClassAlias){
				//found the application config
				$arr_appConfArray = $arr_appConfig;
				break;
			}
		}
		return $arr_appConfArray;
	}
	
	public function getCurrentAppConfByControllerName($s_controllerName)
	{
		
		$arr_appConfArray = $this->getAppConf($s_controllerName, 'controller_class');
		return $arr_appConfArray;
	}
	
	public function getApplicationForms()
	{
		return $this->arr_forms;
	}
	
	public function getFormConfig($s_formId, $b_isFormIdAlias)
	{
		$arr_form = array();
		foreach($this->arr_forms as $index=> $arr_formConfig)
		{
			if($b_isFormIdAlias == true){
				if($arr_formConfig['form_ident_alias'] == $s_formId){
					$arr_form = $arr_formConfig;
				}
			}else{
				if($arr_formConfig['form_ident'] == $s_formId){
					$arr_form = $arr_formConfig;
				}
			}
			
		}
		
		return $arr_form;
	}
	
	public function getFormSubmitArrayIdent()
	{
		return $this->s_formSessionArraySubmitHandler;
	}
	
	public function getFormPostSubmitHandler()
	{
		return $this->s_formSubmitHandlerField;
	}
	
	public function getFormIdentField()
	{
		return $this->s_formIdentField;
	}
	
	public function getApplicationRequestVarName()
	{
		return $this->s_appRequestVarName;
	}
	
	public function getSubApplicationRequestVarName()
	{
		return $this->s_subAppRequestVarName;
	}
	
	public static function getInstance()
	{
		if(!isset(self::$o_instance) ||  self::$o_instance == null) {
			self::$o_instance = new Claycode_configuration();
		}
		return self::$o_instance;
	}
}